package com.asset.dispoal.business.mediation.service.common;

import com.asset.dispoal.business.casedispose.domain.db.mapper.CasedisposeMapper;
import com.asset.dispoal.business.casedispose.dto.CaseDisposalFileResponse;
import com.asset.dispoal.business.mediation.domain.db.entity.ReduceAgreementTmp;
import com.asset.dispoal.business.mediation.domain.db.entity.ReduceOperate;
import com.asset.dispoal.business.mediation.domain.db.entity.ReduceStagingTmp;
import com.asset.dispoal.business.mediation.domain.db.entity.ReduceWayTmp;
import com.asset.dispoal.business.mediation.domain.db.mapper.ReduceAgreementTmpMapper;
import com.asset.dispoal.business.mediation.domain.db.mapper.ReduceOperateMapper;
import com.asset.dispoal.business.mediation.domain.db.mapper.ReduceStagingTmpMapper;
import com.asset.dispoal.business.mediation.domain.db.mapper.ReduceWayTmpMapper;
import com.asset.dispoal.business.mediation.dto.AgreementResponse;
import com.asset.dispoal.business.mediation.dto.ReduceDtailInfoResponse;
import com.asset.dispoal.business.mediation.dto.ReducePreInfo;
import com.asset.dispoal.business.mediation.dto.ReducePresetResponse;
import com.asset.dispoal.business.mediation.dto.StagingInfo;
import com.asset.dispoal.business.mediation.service.converter.MediationConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: yf
 * @date: 2022-08-26 18:59:41
 */
@Repository
public class GetReduceDetailCommand implements BaseCommand<String, Result<ReduceDtailInfoResponse>> {
    @Autowired
    CasedisposeMapper casedisposeMapper;
    @Autowired
    private ReduceOperateMapper reduceOperateMapper;
    @Autowired
    private ReduceStagingTmpMapper reduceStagingTmpMapper;
    @Autowired
    private ReduceWayTmpMapper reduceWayTmpMapper;
    @Autowired
    private ReduceAgreementTmpMapper reduceAgreementTmpMapper;

    @Override
    public Result<ReduceDtailInfoResponse> execute(String id) {
        ReduceOperate reduceOperate = reduceOperateMapper.getLogById(id);
        List<CaseDisposalFileResponse> fileResponses = casedisposeMapper.queryFileByCaseId(reduceOperate.getCaseId());
        List<CaseDisposalFileResponse> reconcileFile = fileResponses.stream().filter(file -> file.getFileType().equals("reconcileFile")).collect(Collectors.toList());
        ReduceDtailInfoResponse reduceDtailInfoResponse = new ReduceDtailInfoResponse();
        reduceDtailInfoResponse.setCaseDisposalFileResponses(reconcileFile);
        //合同id集合
        String agreementIds = reduceOperate.getAgreementIds();
        if (StringUtil.isNotBlank(agreementIds)) {
            List<ReduceAgreementTmp> reduceAgreementTmpList = reduceAgreementTmpMapper.getAgreementInfoByIds(agreementIds);
            for (ReduceAgreementTmp agreementTmp : reduceAgreementTmpList) {
                BigDecimal totalAmount = new BigDecimal(0);
                totalAmount = totalAmount.add(agreementTmp.getLawsuitPrincipalAmount())
                        .add(agreementTmp.getLawsuitInterestAmount())
                        .add(agreementTmp.getLitigationFines());
                agreementTmp.setTotalAmt(totalAmount);
            }
            ReducePresetResponse presetResponse = new ReducePresetResponse();
            List<AgreementResponse> presetResponses = MediationConverter.INSTANCE.dosToResAR(reduceAgreementTmpList);
            presetResponse.setAgreementList(presetResponses);
            //计算总额
            presetResponse.setTotalNum(presetResponses.size());

            BigDecimal totalAmt = BigDecimal.ZERO;
            BigDecimal totalLawsuitPrincipalAm = BigDecimal.ZERO;
            BigDecimal totalLawsuitInterestAmt = BigDecimal.ZERO;
            BigDecimal totalLitigationFines = BigDecimal.ZERO;

            for (AgreementResponse presetRespons : presetResponses) {
                totalAmt = totalAmt.add(new BigDecimal(presetRespons.getTotalAmt()));
                totalLawsuitPrincipalAm = totalLawsuitPrincipalAm.add(new BigDecimal(presetRespons.getLawsuitPrincipalAmount()));
                totalLawsuitInterestAmt = totalLawsuitInterestAmt.add(new BigDecimal(presetRespons.getLawsuitInterestAmount()));
                totalLitigationFines = totalLitigationFines.add(new BigDecimal(presetRespons.getLitigationFines()));
            }
            presetResponse.setTotalAmt(totalAmt.toPlainString());
            presetResponse.setTotalLawsuitInterestAmt(totalLawsuitInterestAmt.toPlainString());
            presetResponse.setTotalLitigationFines(totalLitigationFines.toPlainString());
            presetResponse.setTotalLawsuitPrincipalAm(totalLawsuitPrincipalAm.toPlainString());

            reduceDtailInfoResponse.setReducePresetResponse(presetResponse);
        }

        //减免方式id集合
        String reduceWayIds = reduceOperate.getReduceIds();
        if (StringUtil.isNotBlank(reduceWayIds)) {
            List<ReduceWayTmp> reduceWayTmpList = reduceWayTmpMapper.getReduceWayInfoByIds(reduceWayIds);
            List<ReducePreInfo> preInfoList = MediationConverter.INSTANCE.dosToResRW(reduceWayTmpList);
            reduceDtailInfoResponse.setReducePreInfos(preInfoList);
        }

        //分期id集合
        String stagingIds = reduceOperate.getStagingIds();
        if (StringUtil.isNotBlank(stagingIds)) {
            List<ReduceStagingTmp> reduceStagingTmpList = reduceStagingTmpMapper.getReduceStagingInfoByIds(stagingIds);
            List<StagingInfo> stagingInfoList = MediationConverter.INSTANCE.dosToResRS(reduceStagingTmpList);
            reduceDtailInfoResponse.setStagingInfos(stagingInfoList);
            reduceDtailInfoResponse.setPeriodNum(String.valueOf(stagingInfoList.size()));
        }

        return Result.ok(reduceDtailInfoResponse);
    }
}
